﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core;
using ERP.BusinessEntities;

namespace Components
{
    public partial class LookUpPanel:UserControl
    {
        // the index selected in the listbox
        // maybe of intrest for other classes
        public int SelectedIndex
        {
            get
            {

                if (dataGridView1.CurrentRow != null)
                    return dataGridView1.CurrentRow.Index;
                else return -1;
            }
            set
            {
                try
                {
                    //dataGridView1.Rows[value].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1[1, value];
                }
                catch (Exception ex)
                {
                }
                //dataGridView1.CurrentCell = dataGridView1[0, value];
                
                // musn't be null
                //if (listBox.Items.Count != 0)
                //{ listBox.SelectedIndex = value; }
            }
        } 

        public LookUpPanel()
            :base()
        {
            InitializeComponent();
            dataGridView1.MultiSelect = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            panel1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            dataGridView1.ColumnHeadersVisible = false;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DefaultCellStyle.BackColor = Color.Yellow;

            this.LostFocus += new EventHandler(LookUpPanel_LostFocus);
        }

        void LookUpPanel_LostFocus(object sender, EventArgs e)
        {
            if (lookUpControl.Focused == false)
                lookUpControl.HideSuggestionListBox();
        }

        SGVLookUp lookUpControl;

        public void InitView(SGVLookUp lookup)
        {
            lookUpControl = lookup;
            dataGridView1.Columns.Clear();
            #region ID
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = AppContext.cstID;
            col.Name = col.DataPropertyName;
            col.ReadOnly = true;
            col.Visible = false;
            dataGridView1.Columns.Add(col); 
            #endregion
            if (String.IsNullOrEmpty(lookup.DataSourceColumns) && lookup.DataSourceDisplayColumn != AppContext.cstID)
            {
                col = new DataGridViewTextBoxColumn();
                col.DataPropertyName = lookup.DataSourceDisplayColumn;
                col.Name = col.DataPropertyName;
                col.ReadOnly = true;
                col.Visible = true;
                dataGridView1.Columns.Add(col);
            }
            else
            {
                String[] strColumns = lookup.DataSourceColumns.Split('|');
                foreach (String strCol in strColumns)
                {
                    if (String.IsNullOrEmpty(strCol) || strCol.ToUpper().Equals(AppContext.cstID)) continue;
                    col = new DataGridViewTextBoxColumn();
                    col.DataPropertyName = strCol;
                    col.Name = col.DataPropertyName;
                    col.ReadOnly = true;
                    col.Visible = true;
                    dataGridView1.Columns.Add(col);
                }
            }
        }
        public void SetDataSource(DataTable dt)
        {
            this.SuspendLayout();
            RowCountLabel.Text = dt.Rows.Count.ToString() + " matching.";
            if (dt == null || dt.Rows.Count == 0)
            {
                dataGridView1.Hide();
                this.Size = panel1.Size;
            }
            else
            {
                dataGridView1.Show();
                int iDisplayRowCount = 8;
                if(dt.Rows.Count < 8)
                    iDisplayRowCount = dt.Rows.Count;
                this.Size = new Size(this.Size.Width, panel1.Height + iDisplayRowCount * 25);
            }

            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();
            this.ResumeLayout();
            
        }

        public void MoveSelectionInListBox(int Index)
        {
            // beginning of list
            if (Index <= -1)
            { SelectedIndex = 0; }
            else
                // end of liste
                if (Index > (dataGridView1.Rows.Count - 1))
                    SelectedIndex = (dataGridView1.Rows.Count - 1);
                else
                // somewhere in the middle
                { SelectedIndex = Index; }
        }

        private void OK_Click(object sender, EventArgs e)
        {
            lookUpControl.SelectItemOnList();
        }

        private void New_Click(object sender, EventArgs e)
        {
            BSModuleMapingsInfo maping = AppContext.ModuleMapingCtrl.GetModuleMapingByTable(lookUpControl.DataSourceTable);
            if (maping == null) return;

            AppContext.OpenModule(maping.ModuleName + "Module", 0, Core.Base.ActionType.New, true);
        }
    }
}
